<template>
	<!-- onReachBottom事件在tab切换的时候会失效，故这里用scroll-view -->
	<scroll-view class="workbench" :scroll-y="true" @scrolltolower="shswitch('')">
		<view class="workHeader" style="height: auto;">
			<view class="workHeader_nav">
				<view>盟小二</view>
				<view>
					<image src="../../static/img/ic_search2.png" @click="navpower('search')"></image>
					<image src="../../static/img/ic_information2.png" @click="navpower('information')"></image>
				</view>
			</view>
			<view class="workHeader_num">
				<view class="workHeader_numlist">
					<view class="numlist_val">{{topdata.today_shop}}</view>
					<view class="numlist_title">今日新增用户</view>
				</view>
				<view class="workHeader_numlist">
					<view class="numlist_val">{{topdata.today_box}}</view>
					<view class="numlist_title">今日绑定设备</view>
				</view>
			</view>
			<view class="workHeader_power">
				<view class="powerlist">
					<view @click="navpower('smkh')">
						<image src="../../static/img/work_smkh.png"></image>
						<view>扫码开户</view>
					</view>
					<!-- <view @click="navpower('pfxj')">
						<image src="../../static/img/work_bfxj.png"></image>
						<view>拜访小记</view>
					</view>
					<view @click="navpower('dtzh')">
						<image src="../../static/img/work_dtzh.png"></image>
						<view>地图找户</view>
					</view> -->
				</view>
			</view>
		</view>
		
		<view class="workList">
			<view class="workList_header">
				<view class="workList_top">
					<view class="work_select">
						<text @click="shswitch(0,true)" :class="shIndex==0?'target':''">
							我的门店({{shop.mycount}})
						</text>
						<text @click="shswitch(1,true)" :class="shIndex==1?'target':''">
							外卖门店({{shop.waimaicount}})
						</text>
						<text @click="shswitch(2,true)" :class="shIndex==2?'target':''">
							公海门店({{shop.seacount}})
						</text>
						<text @click="shswitch(3,true)" :class="shIndex==3?'target':''">
							待处理门店({{shop.newshopcount}})
						</text>
					</view>
				</view>
				<view class="workList_conSw" v-show="shIndex==0">
					<contentSw :swlist="conSwlist" @conswitch="conswitch"></contentSw>
				</view>
			</view>
			<view class="workList_con">
				<view v-if="shop.shopdataList.length==0" class="conList_nodata">
					<noData></noData>
				</view>
				<view v-else class="conList">
					<wshoplistCom :shoplistArr="shop.shopdataList" :shoptype="shIndex" :status="shstatusIndex"></wshoplistCom>
					<view style="text-align: center;" v-show="shop.isLoad">
						<u-loading mode="flower" size="40"></u-loading>
					</view>
				</view>
			</view>
		</view>
	</scroll-view>
</template>

<script>
	import { login,index,shoplist,waimaishoplist,seasshop,newshops } from '../../api/index.js'
	import noData from '../../components/data/nodata.vue'
	import wshoplistCom from '../../components/workbench/wshoplist.vue'
	import contentSw from '../../components/workbench/contentSw.vue'
	import config from '../../util/config.js'
	
	export default {
		data() {
			return {
				loginInfo:'',//登录信息
				currentToken:'',
				shIndex:0,//门店切换索引
				shstatusIndex:0,//状态切换索引
				topdata:{
					yesterday_warning:0,
					today_shop:0,
					today_box:0
				},
				shop:{
					shopstatus:0,
					page:0,
					limit:50,
					isLoad:false,//判断之前的请求是否还未结束
					isAll:false,//判断是否加载了全部数据
					shopdataList:[],
					mycount:0,
					waimaicount:0,
					seacount:0,
					newshopcount:0
				},
				conSwlist:[{name:'全部'},{name:'审核中'},{name:'审核失败'},{name:'审核通过'}]
			}
		},
		onLoad() {
			this.init();
		},
		methods: {
			//初始化函数
			init:function(){
				cm.storagFunction({
					method:'getItem',
					indexName:'userInfo',
					success:(e)=>{
						let loginInfo = {};
						let commoninfo = '';
						for (let val of e.info) {
							if(val.platform == 'cm_retail'){
								commoninfo = val;
							}
						}
						if(commoninfo.login_token){
							loginInfo = {
							    "aid": commoninfo.aid,
							    "token": commoninfo.login_token,
							    "sid": commoninfo.sid
							}
						}else{
							let routeInfo = this.$route.query;
							loginInfo = {
							    "aid": routeInfo.aid,
							    "token": routeInfo.token,
							    "sid": routeInfo.sid
							}
						}
						this.currentToken = loginInfo.token
						this.loginInfo = loginInfo;
						this.$storage('global').setItemSync('loginInfo',loginInfo);
						this.$store.dispatch('global/setUser'); //vuex执行获取当前用户信息的方法
					}
				})
				
				// let loginInfo = {
				//     "aid": "10988",
				//     "token": "NDoyOWIzM21sdFF1SHYvc3VVUEsyUi9xUllnUVAyNkpDcldDclhQVUo5T1lIVHZwc21KaHkrUVVjOjY2M2ZWZXVwMlBlZ0k3Ni9mT1MzeFNmeDdLbnIvNWZuc3RCVUFRcDR2dTVuUEtteEtnU0N0UQ==",
				//     "sid": "aaa6080b638888843111f31c651bb556"
				// };
				// this.$storage('global').setItemSync('loginInfo',loginInfo);
				// this.getindex();
				// this.getshoplist();
			},
			//门店切换逻辑与列表切换逻辑统一处理
			shswitch:function(index,isSw){
				if(isSw){ //如果是门店切换逻辑
					this.shIndex = index;
					this.shop.isAll = false;
					this.shop.isLoad = false;
					this.shop.page = 0;
				}else{ //如果是列表加载逻辑
					if(this.shop.isLoad || this.shop.isAll){ //假如之前的请求未完成或者列表数据已经全部加载完毕，则返回空
						console.log("请求未完成或者列表数据已经全部加载完毕")
						return ;
					}
					this.shop.isLoad = true;
				}
				if(index!==0&&index!==1&&index!==2&&index!==3){ //假如传过来的index数量类型有误
					index = this.shIndex;
				}
				if(index == 0){
					this.getshoplist(isSw)
				}else if(index == 1){
					this.getWaimaishoplist(isSw);
				}else if(index == 2){
					this.getSeasshoplist(isSw)
				}else if(index == 3){
					this.getNewshops(isSw)
				}
			},
			conswitch:function(data){
				let index = data.index;
				this.shstatusIndex = index;
				const statusOfindex = { //index对应的status对象
					1:2, //审核中
					2:3, //审核失败
					3:1, //审核通过
					4:4 //未认证
				}
				let status = statusOfindex[index];
				!!status?status:status = 0;// 假如index是其他状态则默认为全部
				
				this.shop.shopstatus = status;
				this.shop.page = 0;
				this.shop.isAll = false;
				if(this.shIndex == 0){
					this.getshoplist(true);
				}
			},
			navpower:function(id){
				if(id == 'smkh'){
					uni.navigateTo({
						url: '../../view/tgqrcode/tgqrcode'
					});
				}else if(id == 'search'){
					uni.navigateTo({
						url: '../../view/search/search'
					});
				}else if(id == 'information'){ //工单消息列表
					window.location.href = `${config.mengxiaoer.BASE_URL + config.mengxiaoer.RETAIL + config.mengxiaoer.VERSION_CODE}/workorder?uid=${this.loginInfo.aid}`
				}else{
					uni.showToast({
						title: '敬请期待',
						icon: 'none'
					});
				}
			},
			//获取顶部数据
			getindex(){
				let that = this;
				index().then((res) => {
					console.log(res);
					if(res.code == 0){
						that.topdata.yesterday_warning = res.data.warning_shop;
						that.topdata.today_shop = res.data.today_shop;
						that.topdata.today_box = res.data.today_box;
					}
				}).catch((e) => {
					console.log(e);
				})
			},
			//获取我的商户列表
			getshoplist:function(isSw){
				let that = this;
				shoplist({
					status:that.shop.shopstatus,
					page:that.shop.page,
					limit:that.shop.limit
				}).then((data) => {
					console.log(data)
					that.shop.mycount = data.data.shop_count;
					// 列表返回数据逻辑统一处理
					that.deallistLogic(data.data,isSw);
				}).catch((e)=>{
					console.log(e);
				})
			},
			//获取外卖门店列表
			getWaimaishoplist:function(isSw){
				let that = this;
				waimaishoplist({
					status:that.shop.shopstatus,
					search:'',
					page:that.shop.page,
					limit:that.shop.limit
				}).then((data) => {
					console.log(data);
					that.shop.waimaicount = data.data.shop_count;
					// 列表返回数据逻辑统一处理
					that.deallistLogic(data.data,isSw);
				}).catch((e)=>{
					console.log(e);
				})
			},
			//获取公海门店列表
			getSeasshoplist:function(isSw){
				let that = this;
				seasshop({
					status:that.shop.shopstatus,
					page:that.shop.page,
					limit:that.shop.limit
				}).then((data) => {
					console.log(data);
					that.shop.seacount = data.data.count;
					// 列表返回数据逻辑统一处理
					that.deallistLogic(data.data,isSw);
				}).catch((e)=>{
					console.log(e);
				})
			},
			//获取平台分发商户列表(待处理门店列表)
			getNewshops:function(isSw){
				let that = this;
				newshops({
					page:that.shop.page,
					limit:that.shop.limit
				}).then((data) => {
					console.log(data);
					that.shop.newshopcount = data.data.shop_count;
					// 列表返回数据逻辑统一处理
					that.deallistLogic(data.data,isSw);
				}).catch((e)=>{
					console.log(e);
				})
			},
			//商户数据列表统一处理
			dealshoplist:function(datalist){
				let statusObj = {
					'0':"",
					'1':"审核成功",
					'2':"审核中",
					'3':"审核失败",
					'4':"未认证"
				}
				let suid = '';
				let realname = '';
				let deallist = datalist.map((item,index,arr)=>{
					let bang = {
						isWaimai:false,
						isPay:false
					};
					if(this.shIndex == 0){ // 我的门店
						suid = item.id;
						realname = item.service_name;
					}else if(this.shIndex == 1){ // 外卖门店
						suid = item.suid;
						realname = item.service_name;
					}else if(this.shIndex == 2){ // 公海门店
						suid = item.id;
						// 公海商户假如service_code>0,则支付已绑定 <=0则支付未绑定，  假如aid>0,则外卖已绑定 <0=则外卖未绑定
						if(item.service_code > 0){
							bang.isPay = true;
						}else{
							bang.isPay = false;
						}
						if(item.aid > 0){
							bang.isWaimai = true;
						}else{
							bang.isWaimai = false;
						}
					}
					return {
						aid:item.service_code||item.aid,//业务员id
						shop_name:item.shop_name,//商户名
						suid:suid,//门店id
						uid:item.uid,//用户id
						realname:realname,//业务员姓名
						address:item.address,//门店地址
						status:item.shop_status||item.status,//审核状态
						statusText:statusObj[item.shop_status],//审核状态文字
						bang:bang,
						mobile:item.mobile,
						distancetime:item.distancetime,
						isovertime:item.isovertime,
						assist_url:item.assist_url
					};
				})
				return deallist;
			},
			// 列表返回数据逻辑统一处理
			deallistLogic:function(data,isSw){
				let dealshoplist;
				if(this.shIndex == 2){ //公海门店
					dealshoplist = this.dealshoplist(data.shoplist);//返回已处理的数据
				}else{
					dealshoplist = this.dealshoplist(data.list);//返回已处理的数据
				}
				if(isSw === true){//判断是否重新加载数据
					this.shop.shopdataList = dealshoplist;
				}else{
					this.shop.shopdataList = this.shop.shopdataList.concat(dealshoplist);
				}
				
				this.shop.page += 1;
				this.shop.isLoad = false;
				if(dealshoplist.length < this.shop.limit){
					this.shop.isAll = true;
				}
			}
		},
		computed:{
			user(){
				return this.$store.state.global.user
			}
		},
		watch:{
			user(){
				this.getindex();
				this.getshoplist(true);
			}
		},
		components:{
			noData,
			wshoplistCom,
			contentSw
		}
	}
</script>

<style lang="scss" scoped>
	@import './workbench.scss';
	.workList_header{
		position: sticky;
		top: 0;
		left: 0;
		right: 0;
		width: 100%;
		z-index: 8;
	}
</style>